#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
监控图像复用数据集生成进度
"""

import json
import os
import time
from datetime import datetime

def monitor_progress():
    """监控处理进度"""
    output_json = "/home/haozhuodi/iclr2026/image_duplication/111/duplication_results.json"
    data_dir = "/home/haozhuodi/iclr2026/image_duplication/111/data"
    
    print("开始监控处理进度...")
    print("按 Ctrl+C 停止监控")
    
    try:
        while True:
            # 检查输出JSON文件
            if os.path.exists(output_json):
                try:
                    with open(output_json, 'r', encoding='utf-8') as f:
                        data = json.load(f)
                    
                    summary = data.get('summary', {})
                    processed = summary.get('total_processed_papers', 0)
                    total = summary.get('total_similar_records', 639)
                    
                    print(f"\r[{datetime.now().strftime('%H:%M:%S')}] 已处理: {processed}/{total} 篇论文 ({processed/total*100:.1f}%)", end='', flush=True)
                    
                    if processed >= total:
                        print(f"\n\n🎉 处理完成！")
                        print(f"成功处理 {processed} 篇论文")
                        break
                        
                except Exception as e:
                    print(f"\n读取结果文件时出错: {e}")
            
            # 检查输出目录中的文件夹数量
            if os.path.exists(data_dir):
                folders = [f for f in os.listdir(data_dir) if f.endswith('_Duplication')]
                print(f"\r[{datetime.now().strftime('%H:%M:%S')}] 已生成 {len(folders)} 个输出文件夹", end='', flush=True)
            
            time.sleep(10)  # 每10秒检查一次
            
    except KeyboardInterrupt:
        print(f"\n\n监控已停止")
        
        # 显示最终统计
        if os.path.exists(data_dir):
            folders = [f for f in os.listdir(data_dir) if f.endswith('_Duplication')]
            print(f"当前已生成 {len(folders)} 个输出文件夹")
            
            # 统计图片文件数量
            total_images = 0
            for folder in folders:
                folder_path = os.path.join(data_dir, folder)
                for root, dirs, files in os.walk(folder_path):
                    total_images += len([f for f in files if f.endswith('.png')])
            
            print(f"总共生成了 {total_images} 个篡改图片文件")

if __name__ == "__main__":
    monitor_progress()
